// pages/decoration-detail/decoration-detail.js
import req from "../../../utils/request"
Page({
  data: {
    id: "2",
    statusText: "待审批",
    statusClass: "pending",
    showFooterButtons: false,
    detail: {
      room: "",
      owner: "",
      documents: [],
      responsiblePerson: "",
      responsiblePhone: "",
      licensePlate: "",
      startDate: "",
      duration: "",
      decorationType: "",
      products: "",
      companyCertificates: [],
      notes: []
    },
  },

  onLoad(options) {
    if (options.id) {
      this.setData({
        id: options.id,
      });
      this.fetchDetailData(options.id);
    }

    // 实际项目中应根据用户角色判断
    const isAdmin = wx.getStorageSync("userRole") === "admin";
    this.setData({
      showFooterButtons: isAdmin && this.data.statusText === "待审批",
    });
  },

  // 获取详情数据
  fetchDetailData(id) {
    wx.showLoading({
      title: "加载中...",
    });

    // 模拟API请求
    setTimeout(async () => {
      // 实际项目中这里应该是API请求
      // const result = await api.getDecorationDetail(id);
      
      // 使用您提供的JSON数据

      const res= await req.get(`/property/renovation/${id}`);

      const data = res.data;
      console.log("--------------"+data)
      
      // 转换状态
      let statusText, statusClass;
      switch(data.approvalStatus) {
        case 0: 
          statusText = "待审批";
          statusClass = "pending";
          break;
        case 1: 
          statusText = "已通过";
          statusClass = "approved";
          break;
        case 2: 
          statusText = "已驳回";
          statusClass = "rejected";
          break;
        default:
          statusText = "待审批";
          statusClass = "pending";
      }
      
      // 准备文档图片
      const documents = [
        data.contractor.license1,
        data.contractor.license2
      ].filter(item => item);
      
      // 准备装修公司证照
      const companyCertificates = [
        data.contractor.license1,
        data.contractor.license2
      ].filter(item => item);
      
      // 备注信息
      const notes = [
        data.approvalNote,
        data.acceptanceNote
      ].filter(item => item);
      
      // 处理业主显示
      const ownerDisplay = data.ownerName 
        ? data.ownerName 
        : data.contractorName + (data.contractor.managerPhone ? ` (${data.contractor.managerPhone.substring(0, 3)}****${data.contractor.managerPhone.substring(7)})` : "");
      
      this.setData({
        statusText,
        statusClass,
        showFooterButtons: wx.getStorageSync("userRole") === "admin" && data.approvalStatus === 0,
        detail: {
          room: data.houseAddress,
          owner: ownerDisplay,
          documents,
          responsiblePerson: data.contractor.managerName,
          responsiblePhone: data.contractor.managerPhone 
            ? `${data.contractor.managerPhone.substring(0, 3)}****${data.contractor.managerPhone.substring(7)}`
            : "",
          licensePlate: data.licensePlate,
          startDate: data.startDate,
          duration: `${data.durationDays}天`,
          decorationType: data.contractor.renovationType,
          products: data.contractor.renovationProducts, // 直接使用字符串
          companyCertificates,
          notes
        }
      });

      console.log('转换后的detail:', this.data.detail);

      wx.hideLoading();
    }, 500);
  },

  // 预览图片
  previewImage(e) {
    const { url, urls } = e.currentTarget.dataset;
    wx.previewImage({
      current: url,
      urls: urls,
    });
  },

  // 驳回申请
  onReject() {
    wx.showModal({
      title: "提示",
      content: "确定要驳回该装修申请吗？",
      success: (res) => {
        if (res.confirm) {
          this.handleReject();
        }
      },
    });
  },

  // 处理驳回逻辑
  handleReject() {
    wx.showLoading({
      title: "处理中...",
    });

    // 模拟API请求
    setTimeout(() => {
      this.setData({
        statusText: "已驳回",
        statusClass: "rejected",
        showFooterButtons: false,
      });

      wx.hideLoading();
      wx.showToast({
        title: "已驳回申请",
        icon: "success",
      });
    }, 1000);
  },

  // 通过申请
  onApprove() {
    wx.showModal({
      title: "提示",
      content: "确定要通过该装修申请吗？",
      success: (res) => {
        if (res.confirm) {
          this.handleApprove();
        }
      },
    });
  },

  // 处理通过逻辑
  handleApprove() {
    wx.showLoading({
      title: "处理中...",
    });

    // 模拟API请求
    setTimeout(() => {
      this.setData({
        statusText: "已通过",
        statusClass: "approved",
        showFooterButtons: false,
      });

      wx.hideLoading();
      wx.showToast({
        title: "已通过申请",
        icon: "success",
      });
    }, 1000);
  },
});